package com.liusand.activity.task;

import com.alibaba.fastjson.JSON;
import com.liusand.activity.service.ActivityService;
import com.liusand.common.constans.RedisPrefixCode;
import com.liusand.entity.pojo.ActivityInfo;
import com.liusand.entity.pojo.RedPackageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.activity.task
 * fileName :RedPackageTask
 * creationTime :2022/12/22 19:45
 */
@Slf4j
@Component
public class RedPackageTask {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Scheduled(fixedRate = 1000)
    public void addRedPackageToRedis(){
        List<ActivityInfo> activityInfoList = activityService.checkActivity();
        if(CollectionUtils.isEmpty(activityInfoList)){
            return;
        }
        List<RedPackageInfo> redPackageInfoList = activityService.findRedPackageInValidity(activityInfoList);
        if(CollectionUtils.isEmpty(redPackageInfoList)){
            return;
        }
        for(RedPackageInfo redPackageInfo : redPackageInfoList){
            if(redPackageInfo == null){
                continue;
            }
            redisTemplate.opsForList().leftPush(RedisPrefixCode.ACTIVITY_RED_PACKAGE_KEY, JSON.toJSONString(redPackageInfo));
        }
        activityService.modifyActivityInfoState(activityInfoList, 1);
        log.info("新活动已添加");
    }

}
